﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using EntLib;
using System.IO;

public partial class Widgets_Admin_rebuildjs : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!Roles.IsUserInRole("Administrator") && Profile.UserName.ToLower() != @"nusstf\cittka")
      {
         EntLibHelper.Log_Action(DateTime.Now, "ACCESS DENIED: " + Request.Url, Profile.UserName);
         return;
      }
      Response.Write("Rebuilding");
      StringBuilder sb = new StringBuilder();
      StringBuilder sb2 = new StringBuilder();
      AppendFile("/js/ieupdate.js", sb, sb2);
      AppendFile("/js/scriptaculous/prototype.js", sb, sb2);
      AppendFile("/js/scriptaculous/effects.js", sb, sb2);
      AppendFile("/js/scriptaculous/dragdrop.js", sb, sb2);
      AppendFile("/js/scriptaculous/controls.js", sb, sb2);
      AppendFile("/js/browser.js", sb, sb2);
      AppendFile("/js/rsa.js", sb, sb2);
      AppendFile("/js/scripts.js", sb, sb2);
      AppendFile("/js/XmlObjtree.js", sb, sb2);
      AppendFile("/js/Generics.js", sb, sb2);
      AppendFile("/js/dragresize.js", sb, sb2);

      //Response.Write("<hr>" + Server.HtmlEncode(sb.ToString()));
      using (StreamWriter sw = new StreamWriter(Server.MapPath("/js/head.js")))
      {
         sw.Write(sb.ToString());
         sw.Write(sb2.ToString());
      }

      sb.Remove(0, sb.Length);
      sb2.Remove(0, sb.Length);

      AppendFile("/js/wz_tooltip.js", sb, sb2);
      AppendFile("/js/tip_balloon.js", sb, sb2);
      AppendFile("/js/mywidgets.js", sb, sb2);
      AppendFile("/js/mypages.js", sb, sb2);
      AppendFile("/js/menu.js", sb, sb2);
      AppendFile("/js/generics2.js", sb, sb2);
      AppendFile("/js/default.aspx.js", sb, sb2);

      using (StreamWriter sw = new StreamWriter(Server.MapPath("/js/foot.js")))
      {
         sw.Write(sb.ToString());
         sw.Write(sb2.ToString());
      }
      Response.Write(" Complete");
   }
   void AppendLine(string Line, StringBuilder sb)
   {
      char LastChar = Line[Line.Length - 1];
      string AllowedChar = ";";
      //if (AllowedChar.IndexOf(LastChar) > -1)
      if (Line.IndexOf("//") < 0 && AllowedChar.IndexOf(LastChar) > -1)
      {
         sb.Append(Line);
      }
      else
      {
         sb.AppendLine(Line);
      }
   }
   void AppendFile(string FileName, StringBuilder sb, StringBuilder sb2)
   {
      using (StreamReader sr = new StreamReader(Server.MapPath(FileName)))
      {
         while (sr.Peek() > 0)
         {
            string s = sr.ReadLine().Trim();
            if (s.Length > 0)
            {
               //sb.AppendLine(s);
               int idx_MassComment = s.IndexOf("/*");
               idx_MassComment = -1;
               if (idx_MassComment > -1)
               {
                  idx_MassComment = s.IndexOf("*");
                  if (idx_MassComment > -1)
                  {
                     //found the mass comment
                     s = s.Substring(idx_MassComment + 2).Trim();
                     if (s.Length > 0)
                     {
                        AppendLine(s, sb);
                     }
                  }
                  else
                  {
                     //if there is a mass comment, we can safely take out those parts until we find a */
                     while (sr.Peek() > 0)
                     {
                        string s2 = sr.ReadLine().Trim();
                        idx_MassComment = s2.IndexOf("*");
                        if (idx_MassComment > -1)
                        {
                           //found the mass comment
                           s2 = s2.Substring(idx_MassComment + 2).Trim();
                           if (s2.Length > 0)
                           {
                              AppendLine(s2, sb);
                           }
                           break;
                        }
                     }
                  }
               }
               else
               {
                  int idx_Comment = s.IndexOf("//");
                  if (idx_Comment > -1)
                  {
                     /*
                     if (idx_MassComment > 0)
                     {
                        s = s.Substring(0, idx_MassComment);
                        if (s.Length > 0)
                           AppendLine(s, sb);
                        AppendLine(s, sb);
                     }
                      */
                     if (idx_Comment > 0)
                     AppendLine(s, sb);
                  }
                  else
                  {
                     AppendLine(s, sb);
                  }
               }  
            }
         }
         AppendLine("installScriptSrc_UpdateDict('" + FileName + "');", sb2);
      }
   }
}
